//
//  TableDetailViewController.m
//  半糖
//
//  Created by RenShuang on 15-9-3.
//  Copyright (c) 2015年 shuang. All rights reserved.
//

#import "TableDetailViewController.h"
#import "BaseHeader.h"

@interface TableDetailViewController ()<UIWebViewDelegate>

@property(nonatomic,retain)UIWebView *myWebView;
@property(nonatomic,retain)UIActivityIndicatorView *activity;
@property(nonatomic,retain)NSURLRequest *request;

@end

@implementation TableDetailViewController

- (void)dealloc
{
    [_myWebView release];
    [_activity release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",self.str);
    NSLog(@"%@",self.titleStr);
    
    self.view.backgroundColor = [UIColor whiteColor];
  
    NSString *urlStr = [NSString stringWithFormat:@"http://m.ibantang.com/topic/detail/%@/",self.str];
    
    _myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, SCREEN_HEIGHT-44)];
    _myWebView.delegate = self;
    _myWebView.opaque = NO;
    _myWebView.scrollView.showsVerticalScrollIndicator = NO;
    _myWebView.scrollView.bounces = NO;
    [_myWebView setScalesPageToFit:YES];
    [self.view addSubview:_myWebView];
    
    _request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [_myWebView loadRequest:_request];
    
    _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activity.center = self.view.center;
    [self.view addSubview:_activity];
    
    [self barCreate];
    
    
}

- (void)barCreate
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    titleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:titleView];
    [titleView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, 20, SCREEN_WIDTH/3, 44)];
    //titleLabel.backgroundColor = [UIColor greenColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"专题详情";
    //label加粗并放大
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    [titleView addSubview:titleLabel];
    [titleLabel release];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(20, 30, 30, 25);
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
    UIButton *scButton = [UIButton buttonWithType:UIButtonTypeSystem];
    scButton.frame = CGRectMake(SCREEN_WIDTH-40, 25, 30, 30);
    [scButton setBackgroundImage:[UIImage imageNamed:@"sc.png"] forState:UIControlStateNormal];
    [scButton addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:scButton];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    shareButton.frame = CGRectMake(SCREEN_WIDTH-40, 25, 30, 30);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
//    [titleView addSubview:shareButton];
}

- (void)shoucang:(UIButton *)sender
{
    NSInteger a = 0;
    firstTableModel *model = [[firstTableModel alloc] init];
    model.title = self.titleStr;
    model.pic = self.picStr;
    NSMutableArray *dataArr = [[DataBase shareDataBase] selectAll];
    for (firstTableModel *modell in dataArr) {
        if ([self.titleStr isEqualToString:modell.title]) {
            a++;
        }
    }
    if (a == 0) {
        [[DataBase shareDataBase] insertStudent:model];
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [aler show];
        [aler release];
     
    }else{
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过了" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [aler show];
        [aler release];
    }
        
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activity startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activity stopAnimating];
//    [_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
//     [_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#000000'"];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_activity stopAnimating];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
